$(document).ready( function(){
	
	$(".toCheckout").on("click", function(){
		var dataString = "&check-order=1";
		var checkoutURL = this.href;
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			success: function(responseJSON){
				if(responseJSON != "")
				{
					response = JSON.parse(responseJSON);
					if(response.error)
					{
						message = '<div class="alert alert-danger">'+response.error+'</div>';
						$("#messageDialog .modal-body").html(message);
						$('#messageDialog').modal('show');
						
						return false;
					}
					else
					{
						window.location = checkoutURL;
					}
				}
				else
				{
					window.location = checkoutURL;
				}
			}
		});
		
		return false;
	});
	
	$("body").on("change","#create_account", function(){
		
		if(this.checked)
		{
			$("#password, #confirm_password").attr("disabled",false);
		}
		else
		{
			$("#password, #confirm_password").attr("disabled","disabled");
		}
	});
	
	$("#checkoutForm").on("change", "input",function(){
		var useID = $(this).attr("id");
		if($(this).attr("type") == "text")
		{
			$("span[bound_to="+useID+"]").html($(this).val());
		}
	});
	
	$("#checkoutForm").on("click", "#nextStep, .changeStep", function(){
		var step = $(this).attr("step");
		
		changeStep(step);
		
		return false;
	});
	
	$("#checkoutForm").on("click", "#checkoutButton", function(){
		var step = 5;
		
		changeStep(step);
		
		return false;
	});
	
	$("#delivery_country_id, #delivery_city_id, #delivery_county_id").select2();
	
	$("#delivery_country_id").on("change", function(){
		$("#ajaxLoader").show();
		var countryID = $(this).val();
		
		dataString = "&get_counties=1&country_id="+countryID;
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			success: function(response){
				$("#delivery_county_id").html(response).attr("disabled", false);
				$("#delivery_county_id").select2();
				$("#ajaxLoader").hide();
			}
		});
	});
	
	$("#delivery_county_id").on("change", function(){
		$("#ajaxLoader").show();
		var countyID = $(this).val();
		
		var countryID = $("#delivery_country_id").val();
		
		dataString = "&get_cities=1&country_id="+countryID+"&county_id="+countyID;
		
		$.ajax({
			type:'POST',
			url: scriptsURL+"libs/ajax/ajax-includes.php",
			data: dataString,
			success: function(response){
				$("#delivery_city_id").html(response).attr("disabled", false);
				$("#delivery_city_id").select2();
				$("#ajaxLoader").hide();
			}
		});
	});
	$("body").on("change", "#use_delivery_address", function(){
		if(this.value == 1)
		{
			$(".deliveryForm").fadeIn();
		}
		else
		{
			$(".deliveryForm").fadeOut();
		}
	});
});


function changeStep(step){
	$("#ajaxLoader").show();
	
	var dataString = $("#checkoutForm").serialize();
	dataString += "&step="+step+"&checkout=1";
	
	$.ajax({
	type:'POST',
	url: scriptsURL+"libs/ajax/ajax-includes.php",
	data: dataString,
	}).done(function(responseJSON)
	{
		response = JSON.parse(responseJSON);
		
		if(response.error_count && response.error_count > 0)
		{
			var message = "";
			$.each( response, function( key, value ) {
				if($("#checkoutForm #"+key).length > 0)
				{
					$("#checkoutForm #"+key).parent().addClass("has-error");
					message += '<div class="alert alert-danger">'+value+'</div>';
				}
			});
			$("#messageDialog .modal-body").html(message);
			$('#messageDialog').modal('show');
			$("#messageDialog .btn").bind("click", function(){
				
			});
		}
		
		if(response.params)
		{
			$.each( response.params, function( key, value ) {
				$(".boundData[bound_to="+key+"]").html(value);
			});
			
			if(response.step == 4)
			{
				if(response.params.payment_method == 3 || response.params.payment_method == 2)
				{
					var orderID = 0;
					
					var dataString = $("#checkoutForm").serialize();
					dataString += "&get_payment_form="+response.params.payment_method;
	
					$.ajax({
					type:'POST',
					url: scriptsURL+"libs/ajax/ajax-includes.php",
					data: dataString,
					}).done(function(formHTML)
					{
						$("#paymentForm").html(formHTML).addClass("has-form");
					});
				}
				else
				{
					$("#paymentForm").html("").removeClass("has-form");
				}
			}
		}
		
		if(response.step)
		{
			
			$("#step_"+step).siblings().addClass("hidden");
			$("#step_"+step).removeClass("hidden");
			
			
			var nextStep = response.step++;
			if(response.step > 4)
			{
				$("#nextStep").addClass("hidden");
				$("#checkoutButton").removeClass("hidden");
				
			}
			else
			{
				$("#nextStep").attr("step", response.step).removeClass("hidden");
				$("#checkoutButton").addClass("hidden");
			}
			
		}
		
		if(response.success)
		{
			if($("#paymentForm").hasClass("has-form") && response.order_id)
			{
				var orderInputValue = $("#paymentForm form .orderID").val();
				$("#paymentForm form .orderID").val(orderInputValue+response.order_id);
				$("#paymentForm form").submit();
			}
			else
			{
				message = '<div class="alert alert-success">'+response.success+'</div>';
							
				$("#messageDialog .modal-body").html(message);
				$('#messageDialog').modal('show');
				$('#messageDialog').on('hidden.bs.modal', function (e) {
					$("#ajaxLoader").show();
					var homeURL = $("#logo").attr("href");
					window.location = homeURL;
					
					$("#ajaxLoader").addClass("unhide");
				});
				
			}
		}
		if(!$("#ajaxLoader").hasClass("unhide"))
		{
			$("#ajaxLoader").hide();
		}
	});
	
}
